package com.fw.ui.view.home;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.fw.ui.bao.MasterDataBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bean.AuthenticationBean;
import com.fw.ui.core.ViewDescription;
import com.fw.ui.core.authentication.AuthenticationUserToken;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.AbstractView;
import com.vaadin.server.Page;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

@org.springframework.stereotype.Component
@Scope(SCOPE_PROTOTYPE)
@ViewDescription(name = "home", path = "/home")
public class HomeView extends AbstractView {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private MasterDataBAO masterDataBAO;
    @Autowired
    private MessageBAO messageBAO;

    /**
     * COMPONENT
     */
    private Panel homePanel = new Panel();

    @Override
    public void updateStrings() {

    }

    @Override
    public void init() {
	this.setSizeFull();

	this.homePanel.setSizeFull();
	this.homePanel.setId("homePanel");

	VerticalLayout mainLayout = new VerticalLayout();
	mainLayout.setSizeFull();
	mainLayout.setSizeUndefined();
	mainLayout.setSpacing(true);
	mainLayout.setWidth("100%");

	this.homePanel.setContent(mainLayout);

	this.addComponent(this.homePanel);
	this.setComponentAlignment(this.homePanel, Alignment.TOP_CENTER);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initValidator() {

    }

    @Override
    public void initData() {

	Authentication authentication = (Authentication) SecurityContextHolder.getContext().getAuthentication();
	if (authentication != null && authentication.isAuthenticated() && authentication instanceof AuthenticationUserToken) {
	    AuthenticationUserToken userToken = (AuthenticationUserToken) authentication;
	    AuthenticationBean authenticationBean = userToken.getAuthenticationBean();
	    if (authenticationBean.getPasswordNotificationExpire() != null
		    && authenticationBean.getPasswordNotificationExpire().booleanValue()) {

		String[] param = new String[] { (authenticationBean.getDiffDays() == null ? "-" : authenticationBean.getDiffDays()
			.toString()) };
		String msg = this.messageBAO.getMessageWithParams("message.notification.password.expire", param);
		Notification notification = new Notification("Notification", msg, Notification.Type.TRAY_NOTIFICATION, true);
		notification.setDelayMsec(5000);
		notification.show(Page.getCurrent());
	    }
	}

    }

    public Panel getCreateBOUserPanel() {
	return homePanel;
    }

    public void setCreateBOUserPanel(Panel addSamplePanel) {
	this.homePanel = addSamplePanel;
    }

}
